#include <iostream>
#include <pm.h>
using namespace std;

BOOL _RegSetValueDWORD(HKEY hRootKey, LPCTSTR lpSubKey, LPCWSTR lpValueName, DWORD dwData);

#define EVENTNAME_BACKLIGHTCHANGEEVENT	TEXT("BackLightChangeEvent")

class BackLight
{
public:
	int IncreaseBrightness();
	int DecreaseBrightness();
};

BOOL SetBrightness(DWORD dwData)
{
	//For 9900 & 7600
	_RegSetValueDWORD(HKEY_CURRENT_USER, _T("ControlPanel\\BackLight\\"), _T("BacklightLevel"), dwData);

	// Notify the Backlight Driver.
	HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, EVENTNAME_BACKLIGHTCHANGEEVENT);
	if (hEvent != NULL)
	{
		SetEvent(hEvent);
		CloseHandle(hEvent);
	}
	return TRUE;
}


int BackLight::DecreaseBrightness()
{
	try
	{
		cout<<"-------------------------------------"<<endl;
		cout<<"BackLight Level: ";
		if(SetDevicePower(L"BKL1:",POWER_NAME,D0)!=ERROR_SUCCESS)
		{
			return 0;
		}

		for (int i=8;i>=0;i--)
		{
			cout<<i<<"...";
			SetBrightness(i);
			Sleep(0.5*1000);
		}
		cout<<endl;
	}
	catch (...)
	{
		return 0;
	}
	return 1;

}

int BackLight::IncreaseBrightness()
{
	try
	{
		cout<<"-------------------------------------"<<endl;
		cout<<"BackLight Level: ";

		for (int i=0;i<9;i++)
		{
			cout<<i<<"...";
			SetBrightness(i);
			Sleep(0.5*1000);
		}
		cout<<endl;
	}
	catch (...)
	{
		return 0;
	}
	return 1;
}

BOOL _RegSetValueDWORD(HKEY hRootKey, LPCTSTR lpSubKey, LPCWSTR lpValueName, DWORD dwData)
{

	DWORD dwDisposition;
	HKEY  hKey= NULL;
	LRESULT lResult = -1;
	lResult =RegCreateKeyEx(hRootKey, lpSubKey, 0, NULL, 0, KEY_SET_VALUE, NULL, &hKey, &dwDisposition);
	if ( lResult == ERROR_SUCCESS)
	{
		if ((const BYTE *)&dwData)
			lResult =RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (const BYTE *)&dwData, sizeof (DWORD));
		RegCloseKey(hKey);
	}
	return (lResult == ERROR_SUCCESS);

}